/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlysieuthi;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import DTO.CMenu;
import DAO.CMenuDAO;
import DAO.SubConfig;
import DTO.CTon_Kho;
import Model.CModel;
import Model.CReport;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author quangphuc
 */
public class FrmMenu extends javax.swing.JFrame implements ActionListener {

    public FrmMenu() {
        initComponents();
        this.createMenu();
        setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
        String str = String.format("kho id %s thang %s nam %s", QuanLySieuThi.kho.getId()
                ,QuanLySieuThi.thang,QuanLySieuThi.nam);
        System.out.println(str);
        loadTonKho();
    }

    public void loadTonKho(){
        CModel _model = new CModel(QuanLySieuThi.user_name);        
        List<CTon_Kho> list = _model.getTonKho(QuanLySieuThi.kho.getId(),QuanLySieuThi.thang,
                QuanLySieuThi.nam) ;
        Vector<Vector> data = new Vector<Vector>();
        int stt = 1;
        //this.pnltonkho
        String strTonKho ="Kho : " + QuanLySieuThi.kho.getTenKho();
        pnltonkho.setBorder(javax.swing.BorderFactory.createTitledBorder(strTonKho));
        for(CTon_Kho item : list){
            Vector<String> row = new Vector<String>();
            row.addElement("" + stt);
            row.addElement("" + item.getTenSanPham());
            row.addElement("" + item.getSoluong());
            row.addElement("" + item.getTonToiThieu());
            row.addElement("" + item.getTonToiDa());
            stt++;
            String strbaoCao = "";
            if(item.getSoluong() < item.getTonToiThieu()){
                strbaoCao = "Sắp hết hàng";
            }
            if(item.getSoluong() == 0){
                strbaoCao = "Hết hàng";
            }
            row.addElement("" + strbaoCao);
            data.add(row);
        }
        Vector<String> columnNames = new Vector<String>();
        columnNames.addElement("STT");
        columnNames.addElement("Tên sản phẩm");        
        columnNames.addElement("Số lượng tồn");
        columnNames.addElement("Tồn tối thiếu");
        columnNames.addElement("Tồn tối đa");
        columnNames.addElement("Ghi chú");
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        this.tbDanhSach.removeAll();
        this.tbDanhSach.setModel(model);
        this.tbDanhSach.setFillsViewportHeight(true);        
        this.tbDanhSach.setEnabled(false);
    }
    
    private void createMenu(){
        this.mnDanhmuc.setVisible(false);
        this.mnHethong.setVisible(false);
        this.mnQuanly.setVisible(false);
        this.mnReports.setVisible(false);
        List<CMenu> list = new ArrayList<CMenu>();
        CMenuDAO dao = new CMenuDAO();
        SubConfig config = SubConfig.getInstance();
        config.init();
        dao.sWhere = String.format(" where group_id = %s", QuanLySieuThi.OrderType);//" where group_id";
        list = dao.getAll(config);
        config.close();
        this.mnDanhmuc.removeAll();
        this.mnHethong.removeAll();
        this.mnQuanly.removeAll();
        this.mnReports.removeAll();
        for(CMenu item : list){
            JMenuItem eMenuItem = null;
            eMenuItem = new JMenuItem(String.format("%s", item.getOrder()));
            eMenuItem.setName(String.format("%s", item.getOrder()));
            eMenuItem.setText(item.getTitle());
            eMenuItem.addActionListener(this);
            int t = item.getLevel();
            switch(t){
                case 1:{
                    if(item.getShow()){
                        this.mnHethong.add(eMenuItem);
                        this.mnHethong.setVisible(true);
                    }
                    break;
                }
                case 2:{
                    if(item.getShow()){
                        this.mnDanhmuc.add(eMenuItem);
                        this.mnDanhmuc.setVisible(true);
                    }
                    break;
                }
                case 3:{
                    if(item.getShow()){
                        this.mnQuanly.add(eMenuItem);
                        this.mnQuanly.setVisible(true);
                    }
                    break;
                }
                case 4:{
                    if(item.getShow()){
                        this.mnReports.add(eMenuItem);
                        this.mnReports.setVisible(true);
                    }
                    break;
                }
            }
        }
        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnltonkho = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSach = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        itemrefresh = new javax.swing.JMenuItem();
        btnChangpass = new javax.swing.JMenuItem();
        item_login = new javax.swing.JMenuItem();
        itemClose = new javax.swing.JMenuItem();
        mnHethong = new javax.swing.JMenu();
        mnDanhmuc = new javax.swing.JMenu();
        mnQuanly = new javax.swing.JMenu();
        mnReports = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        pnltonkho.setBorder(javax.swing.BorderFactory.createTitledBorder("Tồn kho"));

        tbDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbDanhSach);

        javax.swing.GroupLayout pnltonkhoLayout = new javax.swing.GroupLayout(pnltonkho);
        pnltonkho.setLayout(pnltonkhoLayout);
        pnltonkhoLayout.setHorizontalGroup(
            pnltonkhoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnltonkhoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnltonkhoLayout.setVerticalGroup(
            pnltonkhoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnltonkhoLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 21, Short.MAX_VALUE))
        );

        jMenu1.setText("Tài khoản");

        itemrefresh.setText("Refresh");
        itemrefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemrefreshActionPerformed(evt);
            }
        });
        jMenu1.add(itemrefresh);

        btnChangpass.setText("Thay đổi mật khẩu");
        btnChangpass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangpassActionPerformed(evt);
            }
        });
        jMenu1.add(btnChangpass);

        item_login.setText("Đăng nhập/Đăng xuất");
        item_login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item_loginActionPerformed(evt);
            }
        });
        jMenu1.add(item_login);

        itemClose.setText("Thoát");
        itemClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemCloseActionPerformed(evt);
            }
        });
        jMenu1.add(itemClose);

        jMenuBar1.add(jMenu1);

        mnHethong.setText("Hệ thống");
        jMenuBar1.add(mnHethong);

        mnDanhmuc.setText("Quản lý");
        jMenuBar1.add(mnDanhmuc);

        mnQuanly.setText("Chức năng");
        jMenuBar1.add(mnQuanly);

        mnReports.setText("Báo cáo");
        jMenuBar1.add(mnReports);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(pnltonkho, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(pnltonkho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void itemCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemCloseActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_itemCloseActionPerformed

    private void btnChangpassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangpassActionPerformed
        // TODO add your handling code here:
        FrmDoiMatKhau f = new FrmDoiMatKhau();
        f.setVisible(true);
    }//GEN-LAST:event_btnChangpassActionPerformed

    private void item_loginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_item_loginActionPerformed
        // TODO add your handling code here:        
        FrmLogin f = new FrmLogin();
        f.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_item_loginActionPerformed

    private void itemrefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemrefreshActionPerformed
        // TODO add your handling code here:
        CModel _model = new CModel(QuanLySieuThi.user_name);
        _model.refresh();
        _model.Close();
    }//GEN-LAST:event_itemrefreshActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmMenu().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem btnChangpass;
    private javax.swing.JMenuItem itemClose;
    private javax.swing.JMenuItem item_login;
    private javax.swing.JMenuItem itemrefresh;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenu mnDanhmuc;
    private javax.swing.JMenu mnHethong;
    private javax.swing.JMenu mnQuanly;
    private javax.swing.JMenu mnReports;
    private javax.swing.JPanel pnltonkho;
    private javax.swing.JTable tbDanhSach;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        JMenuItem menu = (JMenuItem) e.getSource();        
        int t = Integer.parseInt(menu.getName());
        switch(t){
            case 1:{
                FrmXuatHang f = new FrmXuatHang(this);
                f.setVisible(true);
                break;
            }
            case 2:{
                FrmNhapHang f = new FrmNhapHang(this);
                f.setVisible(true);
                break;
            }
            case 3:{
                FrmHoaDonBH f = new FrmHoaDonBH(this);
                f.setVisible(true);
                break;
            }
            case 4:{
                FrmKhachHang f = new FrmKhachHang(1);
                f.setVisible(true);
                break;
            }
            case 5:{
                FrmKhachHang f = new FrmKhachHang(2);
                f.setVisible(true);
                break;
            }
            case 6:{
                FrmKhuyenMai f = new FrmKhuyenMai();
                f.setVisible(true);
                break;
            }
            case 7:{
                FrmGiaBan f = new FrmGiaBan();
                f.setVisible(true);
                break;
            }
            case 8:{
                FrmLich_su f = new FrmLich_su();
                f.setVisible(true);
                break;
            }
            case 9:{
                FrmSanpham f = new FrmSanpham();
                f.setVisible(true);
                break;
            }
            case 10:{
                FrmDMKho f = new FrmDMKho();
                f.setVisible(true);
                break;
            }
            
            case 11:{
                FrmBaoCao f = new FrmBaoCao(1);
                f.setVisible(true);
                break;
            }
            
            case 12:{
                FrmBaoCao f = new FrmBaoCao(4);
                f.setVisible(true);
                break;
            }
            case 13:{
                FrmBaoCao f = new FrmBaoCao(3);
                f.setVisible(true);
                break;
            }
            case 14:{
                FrmBaoCao f = new FrmBaoCao(2);
                f.setVisible(true);
                break;
            }
            case 15:{
                FrmBaoCao f = new FrmBaoCao(5);
                f.setVisible(true);
                break;
            }
            case 16:{
                FrmBaoCao f = new FrmBaoCao(6);
                f.setVisible(true);
                break;
            }
            case 17:{
                FrmConfig f = new FrmConfig();
                f.setVisible(true);
                break;
            }
            case 18:{
                FrmPhanQuyen f = new FrmPhanQuyen();
                f.setVisible(true);
                break;
            }
            case 19:{
                FrmUser f = new FrmUser();
                f.setVisible(true);
                break;
            }
            case 20:{
                FrmUserGroup f = new FrmUserGroup();
                f.setVisible(true);
                break;
            }
            case 21:{
                FrmNhanVien f = new FrmNhanVien();
                f.setVisible(true);
                break;
            }
        }
    }
}
